package com.jpz.controller;


import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jpz.bean.Course;
import com.jpz.service.CourseService;
import com.jpz.utils.pageshow;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jpz
 * @since 2018-11-07
 */
@Controller
@Scope("prototype")
@RequestMapping("/course")
public class CourseController {
	@Autowired
	CourseService course;
	@RequestMapping("/show")
	public String showlist(int pageindex,Model model) {
		Page<Course> page = new Page<>(pageindex, 4);
		//Page<Course> lisd = course.selectPage(page, null);
		Page<Course> lisd = course.selectPage(page, new EntityWrapper<Course>().eq("del", 0));
		List<Course> lis = lisd.getRecords();
		long total = lisd.getTotal();
		int ii= new Long(total).intValue();  
		//封装页面显示的类的对象
		pageshow<Course> pagebean = new pageshow<>(lis, ii, null, lisd.getCurrent());
		model.addAttribute("pagebean", pagebean);
		return "/course.jsp";
	}
	@RequestMapping("/update")
	public String goupdate(int cid,Model model) {
		//由ID获取对象用于回显
		Course cous = course.selectById(cid);
		model.addAttribute("course", cous);
		return "/courseupdate.jsp";
	}
	@RequestMapping("/reallup")
	public String update(Model model,Course cour) {
		boolean update = course.insertOrUpdate(cour);
		if(update) {
			return "redirect:/course/show?pageindex=1";
		}else {
			return "/courseupdate.jsp";
		}
	}
	@RequestMapping("/del")
	public String del(Model model,int cid) {
		Course course2 = course.selectById(cid);
		course2.setDel(1);
		course.updateById(course2);
		return "redirect:/course/show?pageindex=1";
	}
	@RequestMapping("/add")
	public String add(Model model,Course cour) {
		course.insert(cour);
		return "redirect:/course/show?pageindex=1";
	}
	
}

